﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Collections.Specialized;

namespace Web
{
    public class SqlSetting
    {
        public string Select { get; set; }
        private Where _where = new Where();
        public Where Where {
            get { return _where; }
            set { _where = value; }
        }

        private Where _whereUpdate = new Where();
        public Where WhereUpdate {
            get { return _whereUpdate; }
            set { _whereUpdate = value; }
        }

        private Where _whereDelete = new Where();
        public Where WhereDelete {
            get { return _whereDelete; }
            set { _whereDelete = value; }
        }

        public string From { get; set; }
        public string Table { get; set; }
        public string JsonName { get; set; }

        [XmlAttribute]
        public string DefaultSortName { get; set; }

        [XmlAttribute]
        public string DefaultSortOrder { get; set; }

        private string _sortName;
        public string SortName {
            get {
                if (string.IsNullOrEmpty(_sortName))
                    _sortName = DefaultSortName;
                return _sortName;
            }
            set { _sortName = value; }
        }

        private string _sortOrder;
        public string SortOrder {
            get {
                if (string.IsNullOrEmpty(_sortOrder))
                    _sortOrder = DefaultSortOrder;
                return _sortOrder;
            }
            set { _sortOrder = value; }
        }

        public string OrderBy {
            get { return string.Format(" {0} {1} ", SortName, SortOrder); }
        }

        public int PageNumber { get; set; }

        public int PageSize { get; set; }

        public SqlSetting Parent { get; set; }

        List<SqlSetting> _children = new List<SqlSetting>();
        public List<SqlSetting> Children {
            get { return _children; }
            set {
                _children = new List<SqlSetting>();
                if (value != null) {
                    foreach (var setting in value) {
                        AddChild(setting);
                    }
                }
            }
        }

        public void AddChild(SqlSetting setting) {
            setting.Parent = this;
            _children.Add(setting);
        }

        public List<Field> Dependency { get; set; }

        public List<Field> FieldInserts { get; set; }

        public List<Field> FieldUpdates { get; set; }
    }

    public class Where
    {
        public Where() {
            this.DefaultIngnoreEmpty = true;
            this.DefaultCp = "equal";
        }

        public bool DefaultIngnoreEmpty { get; set; }
        public string DefaultCp { get; set; }

        private List<Field> _fields = new List<Field>();
        public List<Field> Fields {
            get { return _fields; }
            set { _fields = value; }
        }

        public string GetSql() {
            StringBuilder sbSql = new StringBuilder();
            foreach (var field in Fields) {
                if (string.IsNullOrEmpty(field.Cp)) {
                    field.Cp = DefaultCp;
                }
                if (DefaultIngnoreEmpty && string.IsNullOrEmpty(field.Value)) {
                    continue;
                }
                sbSql.Append(field.GetSql());
            }
            return sbSql.ToString();
        }
    }

    public class Field
    {
        [XmlAttribute]
        public string Name { get; set; }
        [XmlAttribute]
        public string Cp { get; set; }

        public string Value { get; set; }

        [XmlAttribute]
        public string DependencyName { get; set; }

        public string GetSql() {
            return Core.Cp.GetSql(this.Cp, this.Name, this.Value);
        }

        [XmlAttribute]
        public bool IsIgnore { get; set; }

    }
}
